package org.itzixi.service;


import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.itzixi.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
@AllArgsConstructor
@Slf4j
public class FileService {

    private final StorageAdapter storageAdapter;


    public String uploadFile(String objectName, InputStream is, boolean needUrl){
        try {
            return storageAdapter.uploadFile(objectName, is, needUrl);
        } catch (Exception e) {
            log.info("上传文件出错了：{},{}", e, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public String uploadFileAndReturnUrl(String objectName, MultipartFile file){
        try {
            return storageAdapter.uploadFileAndReturnUrl(file,  objectName);
        } catch (Exception e) {
            log.info("上传文件出错了：{},{}", e, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public String uploadFile(String objectName,
                           String fileName, boolean needUrl){
        try {
            return storageAdapter.uploadFile(objectName,  fileName, needUrl);
        } catch (Exception e) {
            log.info("上传文件出错了：{},{}", e, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public String getUrl(String objectName){
        String url = storageAdapter.getUrl(objectName);
        return url;
    }


}
